// pages/hospital/hospital.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    levelArr: [
      { value: 0, name: '请选择' },
      { value: 1, name: '三级甲等' },
      { value: 2, name: '三级乙等' },
      { value: 3, name: '三级丙等' },
      { value: 4, name: '二级甲等' },
      { value: 5, name: '二级乙等' },
      { value: 6, name: '二级丙等' },
      { value: 7, name: '一级甲等' },
      { value: 8, name: '一级乙等' },
      { value: 9, name: '一级丙等' },
      { value: 10, name: '其他' },
    ],
    levelIdx: 0,
    flag: 0,
    flag1: 0,
    chooseLoaction: 0,
    location: '',
    longitude: 123.287682,
    latitude: 44.124004,
    rawData: [],//后台获取的原始数组
    multiObjArray: [], //整理后在页面循环的数组
    multiIndex: [0, 0],
    comArr: [],
    gridArr: [],
    newArr: [],
    g_id: '',
    c_id: '',
  },

  onLoad(opts) {

  },

  onShow() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];   //上一页
    prevPage.setData({
      status: 1
    })
  },

  levelChange(e) {
    console.log(e)
    this.setData({
      levelIdx: e.detail.value,
      flag1: 1
    })
  },

  formSubmit(e) {
    console.log(e)
    let { h_name, bed_num, cover_area, legal_person, legal_person_phone, party_master, party_master_phone, labour_union_master, labour_union_master_phone, labour_vip_num, party_num, culture_remark, address, remark, urgent_room_tel } = e.detail.value
    let access_token = wx.getStorageSync('access_token')
    let level = this.data.levelArr[this.data.levelIdx].value
    if (e.detail.value.h_name == '') {
      wx.showToast({
        title: '医院名称不能为空！',
        icon: 'none'
      })
      return false
    }
    if (level == '') {
      wx.showToast({
        title: '请选择医院等级！',
        icon: 'none'
      })
      return false
    }
    if (bed_num == '') {
      wx.showToast({
        title: '床位数量不能为空！',
        icon: 'none'
      })
      return false
    }
    if (address == '') {
      wx.showToast({
        title: '详细地址不能为空！',
        icon: 'none'
      })
      return false
    }
    if (legal_person == '') {
      wx.showToast({
        title: '法人代表不能为空！',
        icon: 'none'
      })
      return false
    }
    wx.showLoading({
      title: '正在提交...',
      mask: true
    })

    wx.request({
      url: 'https://baokang.wshoto.com/wx/hospitaladd',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        access_token: access_token,
        level: level,
        // x_coordinate: x_coordinate,
        // y_coordinate: y_coordinate,
        h_name: h_name,
        bed_num: bed_num,
        cover_area: cover_area,
        legal_person: legal_person,
        legal_person_phone: legal_person_phone,
        party_master: party_master,
        party_master_phone: party_master_phone,
        labour_union_master: labour_union_master,
        labour_union_master_phone: labour_union_master_phone,
        labour_vip_num: labour_vip_num,
        party_num: party_num,
        culture_remark: culture_remark,
        address: address,
        remark: remark,
        urgent_room_tel: urgent_room_tel,
      },
      success: (res) => {
        console.log(res)
        wx.hideLoading()
        if (res.data.code == 1) {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
            success: (res) => {
              if (res.confirm) {
                wx.navigateBack()
              }
            }
          })
        } else {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
          })
        }
      }
    })
  }
})